/*
 * CSocketUtils.cpp
 *
 *  Created on: 2011-9-7
 *      Author: stargui
 */

#include "CPSocketUtils.h"
//#define DEBUG_ERROR(p)    // printf("%s",#p)
//#define //DEBUG_WARNING(p1,p2) // printf("%s",#p);
CPSocketUtils::CPSocketUtils()
{

}

CPSocketUtils::~CPSocketUtils()
{

}

int CPSocketUtils::OpenTcpSocket()
{
	int reuse = 1;
	int handle = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
	SetNonBlockSocket(handle);
	return handle;
}

int CPSocketUtils::OpenUdpSocket()
{
	int reuse = 1;
	int handle = socket(AF_INET, SOCK_DGRAM, 0);
	setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
	//SetNonBlockSocket(handle);
	return handle;
}

int CPSocketUtils::OpenRawSocket()
{
	int reuse = 1;
	int handle = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
	setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
	SetNonBlockSocket(handle);
	return handle;
}
int CPSocketUtils::OpenLinkSocket()
{
	int handle;

	if ((handle = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0)
	{
		DEBUG_ERROR("socket call failed");
		return -1;
	}
	SetNonBlockSocket(handle);
	return handle;
}

bool CPSocketUtils::SetNonBlockSocket(int handle)
{
	fcntl(handle, F_SETFL, fcntl(handle, F_GETFL) | O_NONBLOCK);
	return true;
}

bool CPSocketUtils::ListenUdpSocket(int &handle, unsigned short port)
{
	struct sockaddr_in server;
	memset(&server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = htonl(INADDR_ANY);
	server.sin_port = htons(port);
	if (::bind(handle, (struct sockaddr *) &server, sizeof(server)) != 0)
	{
		return false;
	}
	return true;
}

bool CPSocketUtils::ListenTcpSocket(int &handle, unsigned short port)
{
	struct sockaddr_in server;
	memset(&server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = htonl(INADDR_ANY);
	server.sin_port = htons(port);
	int	opt = 1;
	int	len = sizeof( int );

	if ( setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &opt, len ) < 0 )
	{
		if ( handle )
		{
			close( handle );
			handle 	= -1;
		}
		return false;
	}


	if (::bind(handle, (struct sockaddr *) &server, sizeof(server)) == 0)
	{
		return (listen(handle, 64) == 0);
	}
	return false;
}

bool CPSocketUtils::ListenLinkSocket(int &handle, char* szInterface)
{
	struct sockaddr_ll addr_ll;

	addr_ll.sll_family = AF_PACKET;
	addr_ll.sll_protocol = htons(ETH_P_IP);

	Getifindex(szInterface, &addr_ll.sll_ifindex);

	//DEBUG_WARNING("eth0 ifindex is %d", addr_ll.sll_ifindex);

	if (::bind(handle, (struct sockaddr *) &addr_ll, sizeof(addr_ll)) < 0)
	{
		//DEBUG_ERROR("bind call failed");
		close(handle);
		handle = -1;
		return false;
	}
	return true;
}

void CPSocketUtils::CloseSocket(int &handle)
{
	if(handle > 0)
	{
		close(handle);
		handle			= -1;
	}
}

int CPSocketUtils::SetTcpSocketTimeout(int handle, int idle, int interval, int times)
{
	int keepAlive = 1;
	setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, (void *) &keepAlive, sizeof(keepAlive));
	setsockopt(handle, SOL_TCP, TCP_KEEPIDLE, (void*) &idle, sizeof(idle));
	setsockopt(handle, SOL_TCP, TCP_KEEPINTVL, (void *) &interval, sizeof(interval));
	setsockopt(handle, SOL_TCP, TCP_KEEPCNT, (void *) &times, sizeof(times));
	return true;
}

int CPSocketUtils::SetSocketBuffer(int handle, int rcvbuf, int sndbuf)
{
	setsockopt(handle, SOL_SOCKET, SO_RCVBUF, (void *) &rcvbuf, sizeof(rcvbuf));
	setsockopt(handle, SOL_SOCKET, SO_SNDBUF, (void *) &sndbuf, sizeof(sndbuf));
	return 0;
}

int CPSocketUtils::AcceptTcpSocket(int handle, int timeout)
{
	fd_set rset;
	struct timeval tv;
	FD_ZERO(&rset);
	FD_SET(handle, &rset);
	tv.tv_sec = timeout / 1000000;
	tv.tv_usec = timeout % 1000000;
	int	iresult	= select(handle + 1, &rset, NULL, NULL, &tv);
	if (iresult > 0)
	{
		if (FD_ISSET(handle, &rset))
		{
			struct sockaddr_in client;
			socklen_t length = sizeof(client);
			return accept(handle, (struct sockaddr*) &client, &length);
		}
	}
	else if(iresult == 0)
	{
		return 0;
	}
	return -1;
}

int CPSocketUtils::ConnectTcpSocket(int handle, const char *address, unsigned short port)
{
	int status = 0;
	struct sockaddr_in client;
	memset(&client, 0, sizeof(client));
	client.sin_family = AF_INET;
	client.sin_port = htons(port);
	inet_pton(AF_INET, address, &client.sin_addr);
	status = connect(handle, (struct sockaddr *) &client, sizeof(client)) ;

	if ((errno == EINPROGRESS)|| (errno == EALREADY) || (errno == EISCONN) || (errno == EACCES) || (status != -1))
	{
		if((status != -1)|| (errno == EISCONN) || (errno == EACCES))
		{
			return 0;
		}
		fd_set rset,wset;
		struct timeval tv;
		FD_ZERO(&rset);
		FD_ZERO(&wset);
		FD_SET(handle, &rset);
		FD_SET(handle, &wset);
		tv.tv_sec = 1;
		tv.tv_usec = 0;
		select(handle + 1, &rset, &wset, NULL, &tv);
		if(FD_ISSET(handle,&rset) || FD_ISSET(handle,&wset))
		{
			return 0;
		}
		////DEBUG_WARNING("--status:[%d]-EISCONN:[%d][%d][%d][%d]-errno:[%d]-\n",status,EISCONN,EACCES,EINPROGRESS,EALREADY,errno);
		return 1;
	}

	return -1;

}

int CPSocketUtils::Send(
						int handle,
						const char *buf,
						int len,
						int flags,
						const int selectSec,
						const int selectUsec)
{
	int		sendtotal	= -1;
	int		sendlen	= 0;
	fd_set wset;
	struct timeval tv;
	FD_ZERO(&wset);
	FD_SET(handle, &wset);
	tv.tv_sec = selectSec / 1000000;
	tv.tv_usec = selectUsec % 1000000;
	int	result	= select(handle + 1, NULL, &wset, NULL, &tv);
	if ( result > 0)
	{
		if (FD_ISSET(handle, &wset))
		{
			sendtotal		= 0;
			while(sendtotal < len)
			{
				sendlen	 = send(handle, (buf + sendtotal), (len - sendtotal) , flags);
				if(sendlen <= 0)
				{
					perror("-----[send]----:");
					return -1;
				}
				sendtotal	+=  sendlen;
			}
		}
	}
	else if(result == 0)
	{
		//DEBUG_WARNING("-------select---------\n");
		perror("-----[select]----:");
	}
	else
	{
		//DEBUG_WARNING("-------select---------\n");
				perror("-----[select]----:");
	}

	return sendtotal;
}

int CPSocketUtils::Send(
						int handle,
						const char *headbuf,
						const char *databuf,
						int headlen,
						int datalen,
						int flags,
						const int selectSec,
						const int selectUsec)
{
	int		sendtotal	= -1;
	int		sendlen	= 0;
	fd_set rset;
	struct timeval tv;
	FD_ZERO(&rset);
	FD_SET(handle, &rset);
	tv.tv_sec = selectSec / 1000000;
	tv.tv_usec = selectUsec % 1000000;
	int		sendfailedcount	= 0;
	int		result	= 0;
	int		timeoutcount		= 3;

SENDTIMEOUT:
	result			= select(handle + 1, NULL, &rset, NULL, &tv);

	if (result > 0)
	{
		if (FD_ISSET(handle, &rset))
		{
			if((headbuf != NULL) && (headlen > 0))
			{
				sendtotal		= 0;
				while(sendtotal < headlen)
				{
					sendlen	= send(handle,(headbuf + sendtotal),(headlen - sendtotal) , flags);
					if(sendlen > 0)
					{
						sendtotal		+= sendlen;
					}
					else if(sendlen < 0)
					{
						perror("-----[send head]----:");
						if(errno == EAGAIN)
						{
							CPSelectUtils::mSleep(200);
							if(sendfailedcount++ > 4)
							{
								return -1;
							}
							continue;
						}
						else
						{
							return -1;
						}
					}
					else
					{
						return -1;
					}
				}
			}


			sendfailedcount	= 0;
			if((databuf != NULL) && (datalen > 0))
			{
				sendtotal		= 0;
				while(sendtotal < datalen)
				{
					sendlen	 = send(handle, (databuf + sendtotal), (datalen - sendtotal) , flags);
					if(sendlen < 0)
					{
						if(errno == EAGAIN)
						{
							////DEBUG_WARNING("---sendlen:[%d]-------------\n",sendlen);
							CPSelectUtils::mSleep(30);
							sendlen	= 0;
							if(sendfailedcount++ > 20)
							{
								perror("-----[send data]----:");
								//DEBUG_WARNING("----------------\n");
								return -1;
							}
						}
						else
						{
							perror("-----[send data]----:");
							//DEBUG_WARNING("----------------\n");
							return -1;
						}
					}
					sendtotal	+=  sendlen;
					if(sendtotal < datalen)
					{
						CPSelectUtils::mSleep(50);
					}
				}
			}
		}
	}
	else
	{
		if((errno == EAGAIN) || (0 == result))
		{
			////DEBUG_WARNING("result:[%d]----selectSec:[%d]---selectUsec:[%d]\n",result,selectSec,selectUsec);
			CPSelectUtils::mSleep(100);
			sendtotal			= 0;
			if(timeoutcount-- < 1)
			{
				//DEBUG_WARNING("------timeoutcount----------\n");
				perror("-----[send data]----:");
				return -1;
			}
			CPSelectUtils::mSleep(200);
			goto SENDTIMEOUT;
		}
	}

	return sendtotal;
}

int CPSocketUtils::Send(
						int handle,
						char *headbuf,
						char *databuf,
						int &headlen,
						unsigned int &datalen,
						int	 &headsend,
						int	 &datasend,
						int flags,
						const int selectSec,
						const int selectUsec)
{
	unsigned int		sendtotal	= 0;
	int		sendlen				= 0;
	fd_set rset;
	struct timeval tv;
	FD_ZERO(&rset);
	FD_SET(handle, &rset);
	tv.tv_sec = selectSec / 1000000;
	tv.tv_usec = selectUsec % 1000000;
	int		result	= select(handle + 1, NULL, &rset, NULL, &tv);
	if (result > 0)
	{
		if (FD_ISSET(handle, &rset))
		{
			sendtotal		= 0;
			while(sendtotal < headlen)
			{
				sendlen	= send(handle,(headbuf + sendtotal),(headlen - sendtotal) , flags);

				if(sendlen > 0)
				{
					sendtotal		+=	sendlen;
					headsend		= 1;
				}
				else
				{
					 if((sendlen < 0)&& (errno == EAGAIN))
					 {
						 CPSelectUtils::mSleep(100);
						 ////DEBUG_WARNING("------------------------\n");
						 continue;
					 }
					// //DEBUG_WARNING("---headbuf:[%s]----\n",headbuf);
					perror("-----[send head]----:");
					return -1;
				}
			}

			headbuf		= NULL;
			headlen		= 0;
			headsend		= 0;

			sendtotal		= 0;
			sendlen		= 0;
			while(sendtotal < datalen)
			{
				sendlen	 = send(handle, (databuf + sendtotal), (datalen - sendtotal) , flags);
				////DEBUG_WARNING("----------sendlen:[%d]--------------\n",sendlen);
				if(sendlen > 0)
				{
					sendtotal	+=  sendlen;
					datasend		= 1;
				}
				else
				{
					if((sendlen <0) && (errno == EAGAIN))
					{
						//datalen		-= sendtotal;
						//datasend		= 1;
						//databuf		= &databuf[sendtotal];
						CPSelectUtils::mSleep(10);
						////DEBUG_WARNING("------------------------\n");
						//return 	0;
						continue;
					}
					perror("-----[send data]----:");
					return -1;
				}
				if(sendtotal < datalen)
				{
					CPSelectUtils::mSleep(100);
				}
			}

			databuf		= NULL;
			datalen		= 0;
			datasend		= 0;
		//	//DEBUG_WARNING("-------datalen:[%d]-----------------\n",datalen);
		}
	}
	else
	{
		if((result == 0) || (errno == EAGAIN))
		{
			headsend		= 1;
			datasend		= 1;
			sendtotal		= 0;
		}
	//	//DEBUG_WARNING("-------select------errno-[%d]--\n",errno);
		perror("-----[select]----:");
	}

	return sendtotal;
}

int CPSocketUtils::Recv(
						int handle,
						char *buf,
						int len,
						int flags)
{
	return recv(handle, buf, len, flags);
}

int CPSocketUtils::Recv(
						int handle,
						char *buf,
						int len,
						int flags,
						const int selectSec,
						const int selectUsec)
{
	fd_set rset;
	struct timeval tv;
	FD_ZERO(&rset);
	FD_SET(handle, &rset);
	tv.tv_sec = selectSec / 1000000;
	tv.tv_usec = selectUsec % 1000000;
	int		result	= select(handle + 1, &rset, NULL, NULL, &tv);
	int		recvlen	= 0;
	if(result > 0)
	{
		if(FD_ISSET(handle,&rset))
		{
			recvlen	=  recv(handle, buf, len, flags);
			if(recvlen > 0)
			{
				return recvlen;
			}
			else
			{
				return -1;
			}
		}
	}
	else if(result == 0)
	{
		return 0;
	}

	return -1;


//
//	CPSelectUtils selectutils;
//	selectutils.InsertfdSets(handle,P_FD_SETS_READ);
//	selectutils.InsertfdSets(handle,P_FD_SETS_ERROR);
//	int		iresult	= selectutils.Select(selectSec,selectUsec);
//	int		recvlen	= 0;
//	if(iresult > 0)
//	{
//		if(selectutils.CheckSets(handle,P_FD_SETS_READ))
//		{
//			recvlen	=  recv(handle, buf, len, flags);
//			if(recvlen > 0)
//			{
//				return recvlen;
//			}
//			else
//			{
//				return -1;
//			}
//		}
//		else if(selectutils.CheckSets(handle,P_FD_SETS_ERROR))
//		{
//			//DEBUG_WARNING("-----------------------------------------\n");
//			return -1;
//		}
//	}
//	else if(iresult < 0)
//	{
//		//DEBUG_WARNING("-----------------------------------------\n");
//		return -1;
//	}
//
//	return 0;
}

void CPSocketUtils::ParseAddress(std::string &value, std::string &address, unsigned short &port)
{
	size_t pos = 0;
	if ((pos = value.find(":")) != value.npos)
	{
		address = value.substr(0, pos);
		port = atoi(value.substr(pos + 1).c_str());
	}
	else
	{
		address = "127.0.0.1";
		port = 54321;
	}
}


int	CPSocketUtils::SetSockLingerAttr(
									int handle,
									const int lingerOnOff,
									const  int lingerSec)
{
	if(handle > 0)
	{
		struct linger lingerSet;

		memset( &lingerSet, 0x0, sizeof( linger ) );
		lingerSet.l_onoff = lingerOnOff;
		lingerSet.l_linger = lingerSec;

		int	setRet = setsockopt( handle, SOL_SOCKET, SO_LINGER, ( char* )&lingerSet, sizeof( linger ) );
		return setRet;
	}
	return -1;
}


int	CPSocketUtils::SetSockSendTimeOut(
								int handle,
								const int timeoSec,
								const int timeoUsec )
{
	if(handle > 0)
	{
		struct timeval	sendTimeo;

		memset( &sendTimeo, 0x0, sizeof( timeval ) );
		sendTimeo.tv_sec = timeoSec;
		sendTimeo.tv_usec = timeoUsec;

		int	setRet = setsockopt( handle, SOL_SOCKET, SO_SNDTIMEO, ( char* )&sendTimeo, sizeof( timeval ) );

		return setRet;
	}
	return -1;

}

int	CPSocketUtils::SetSockRecvTimeOut(
								int handle,
								const int timeoSec,
								const int timeoUsec )
{
	if(handle > 0)
	{
		struct timeval	recvTimeo;

		memset( &recvTimeo, 0x0, sizeof( timeval ) );
		recvTimeo.tv_sec = timeoSec;
		recvTimeo.tv_usec = timeoUsec;

		int	setRet = setsockopt( handle, SOL_SOCKET, SO_RCVTIMEO, ( char* )&recvTimeo, sizeof( timeval ) );
		return setRet;
	}
	return -1;

}

int	CPSocketUtils::BindSocketToDevice(
									int handle,
									const char* pDev)
{
	if(handle > 0)
	{
		struct ifreq	devInfo;
		memset(&devInfo,0,sizeof(ifreq));

		strcpy(devInfo.ifr_name, pDev);
		int	setRet = setsockopt(handle, SOL_SOCKET, SO_BINDTODEVICE, (char*)&devInfo, sizeof(devInfo) );
		return setRet;
	}
	return -1;
}

int CPSocketUtils::Getifindex(const char *interface, int *ifindex)
{
	int fd;
	struct ifreq ifr;
	//struct sockaddr_in *our_ip;

	memset(&ifr, 0, sizeof(struct ifreq));
	if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0)
	{
		ifr.ifr_addr.sa_family = AF_INET;
		strcpy(ifr.ifr_name, interface);

		if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0)
		{
			//DEBUG_WARNING("adapter index %d", ifr.ifr_ifindex);
			*ifindex = ifr.ifr_ifindex;
		}
		else
		{
			DEBUG_ERROR("SIOCGIFINDEX failed!");
			close(fd);
			return -1;
		}

	}
	else
	{
		DEBUG_ERROR("socket failed!");
		return -1;
	}
	return 0;
}

int	CPSocketUtils::SetSockNoDelay(int sockFd )
{
	if (sockFd <= 0 )
	{
		return	( -1 );
	}

	int	tcpNoDelay = 1;

	int setRet = setsockopt( sockFd, SOL_SOCKET, TCP_NODELAY, (const char*)&tcpNoDelay, sizeof( tcpNoDelay ) );

	return	( setRet );
}


int CPSocketUtils::SendPkg(int socket,char *szbuff,int szSendbuflen,struct sockaddr_in* to,int timeUsec)
{
	if(!szbuff || szSendbuflen < 1 || socket <= 0)
	{
		return -1;
	}
	int		iTimeoutTime = 0;
	fd_set	fd_writes;

	timeval timeval;

	FD_ZERO(&fd_writes);
	FD_SET(socket, &fd_writes);

	timeval.tv_sec = timeUsec / 1000000;
	timeval.tv_usec = timeUsec % 1000000;

	int iSelect = 0;
	int	nLenSend = -1;
	int	 sendtotal	= 0;
//	char buftemp[]		= "123";
	while (1)
	{
		iSelect = select(socket+1, NULL, &fd_writes, NULL, &timeval);
		if (iSelect > 0)
		{
			//printf("send data1");
			if(FD_ISSET(socket, &fd_writes))
			{
				//nLenSend = write(socket,(szbuff + sendtotal),(szSendbuflen - sendtotal));
				nLenSend = sendto(socket,(szbuff + sendtotal),(szSendbuflen - sendtotal),0,(struct sockaddr*)to,sizeof(struct sockaddr));
				if (nLenSend <= 0)
				//if ((nLenSend = write(socket,buftemp,strlen(buftemp)))== -1)
				{
					printf("[%s %d]: Error: Failed in sendto (err:%s)\n",__func__,__LINE__, strerror(errno));
					return -1;
				}
				//char *peerip								= inet_ntoa(to->sin_addr);
				//printf("[%s %d]: send data length is %d sin_port:[%d],ip:%s\n",__func__,__LINE__,nLenSend,ntohs(to->sin_port),peerip);
				sendtotal		+= nLenSend;
				if (sendtotal >= szSendbuflen)
				{
					break;
				}
				iTimeoutTime = 0;
			}
		}
		else if (iSelect == 0/* && iTimeoutTime < 5*/)
		{
			iTimeoutTime ++;
			FD_ZERO(&fd_writes);
			FD_SET(socket, &fd_writes);
			CPSelectUtils::mSleep(1000);
			if (iTimeoutTime == 2)
			{
				printf("[%s %d]: Error: SendPkg Select timeout!\n",__func__,__LINE__);
				return 0;
			}

		}
		else
		{
			printf("[%s %d]: Error: Failed in SendAll (err:%s)\n", __func__,__LINE__, strerror(errno));
			return -1;
		}
	}
	return nLenSend;
}

int CPSocketUtils::RecvPkg(int socket,struct sockaddr_in* from, char* szbuff,  int length,int timeUsec)
{
		int		iTimeoutTime = 0;

		struct sockaddr_in* pfromaddr = NULL;
		struct sockaddr_in  fromaddr;

		if(from)
		{
			pfromaddr = from;
		}
		else
		{
			pfromaddr = &fromaddr;
		}

		fd_set	fd_Recvs;

		timeval timeval;

		FD_ZERO(&fd_Recvs);
		FD_SET(socket, &fd_Recvs);

		timeval.tv_sec = timeUsec / 1000000;
		timeval.tv_usec = timeUsec % 1000000;

		int iSelect = 0;
		int	nLenRecv = -1;
		//char recvBuff[1024];

		//SOCKADDR_IN	addrRemote;

		int  nSenderSize = sizeof(struct sockaddr_in);

		while (1)
		{
			iSelect = select(socket+1, &fd_Recvs, NULL, NULL, &timeval);
			if (iSelect > 0)
			{
				if(FD_ISSET(socket, &fd_Recvs))
				{
					if ((nLenRecv = recvfrom(socket, szbuff, length, 0, (struct sockaddr*)pfromaddr,(socklen_t*)&nSenderSize))== -1)
					{
						printf("[%s %d]: Error: Failed in ReceiveAll (err:%s)\n",__func__,__LINE__, strerror(errno));
						return -1;
					}
					if (nLenRecv > 0)
					{
						szbuff[nLenRecv] = '\0';
						//memcpy(pPkg,recvBuff,sizeof(statepackage));
						//char szRemoteIP[32];
						//strcpy(pszDestIP,inet_ntoa(from.sin_addr));
						//*length = nLenSend;
						printf("[%s %d]: [%s] send to me\n",__func__,__LINE__,inet_ntoa(pfromaddr->sin_addr));

						break;
					}
					iTimeoutTime = 0;
				}
			}
			else if (iSelect == 0/* && iTimeoutTime < 5*/)
			{
				iTimeoutTime ++;

				FD_ZERO(&fd_Recvs);
				FD_SET(socket, &fd_Recvs);
				CPSelectUtils::mSleep(1000);
				if (iTimeoutTime == 2)
				{
					//printf("Error: RecvPkg Select timeout!\n");
					return 0;
				}

			}
			else
			{
				printf("[%s %d]:Error: Failed in ReceiveAll (err:%s)\n",__func__,__LINE__, strerror(errno));

				return -1;
			}
		}
		return nLenRecv;
}


char *CPSocketUtils::GetIpbySocket(const int handle)
{
	struct sockaddr_in clientaddr = {0};
	socklen_t		size;
	size								= sizeof(sockaddr_in);
	char	*peerip					= NULL;
	getpeername(handle,(sockaddr*)&clientaddr,&size);
	peerip								= inet_ntoa(clientaddr.sin_addr);

	return peerip;

}

int CPSocketUtils::GetDesAddrbySocket(const int handle,struct sockaddr_in *desaddr)
{
	socklen_t		size;
	size								= sizeof(sockaddr_in);
	getpeername(handle,(sockaddr*)desaddr,&size);
	return 0;
}

int CPSocketUtils::GetSrcAddrbySocket(const int handle,struct sockaddr_in *srcaddr)
{
	struct sockaddr_in addr;
	memset(&addr,0,sizeof(addr));
	int len = sizeof(addr);
	getsockname(handle,(struct sockaddr*)srcaddr,(socklen_t*)&len);
	return 0;
}

//只能获取网关的mac
//int CPSocketUtils::GetMacbySocekt(const int handle,char *mac)
//{
//	int ret =0;
//	struct arpreq arpreq;
//	struct sockaddr_in dstadd_in;
//	socklen_t  len = sizeof( struct sockaddr_in );
//	memset( &arpreq, 0, sizeof( struct arpreq ));
//	memset( &dstadd_in, 0, sizeof( struct sockaddr_in ));
//	if( getpeername( handle, (struct sockaddr*)&dstadd_in, &len ) < 0 )
//		err_err("getpeername()");
//	else
//	{
//		memcpy( &arpreq.arp_pa, &dstadd_in, sizeof( struct sockaddr_in ));
//		strcpy(arpreq.arp_dev, "eth1");
//		arpreq.arp_pa.sa_family = AF_INET;
//		arpreq.arp_ha.sa_family = AF_UNSPEC;
//		if( ioctl( handle, SIOCGARP, &arpreq ) < 0 )
//			err_err("ioctl SIOCGARP");
//		else
//		{
//			unsigned char* ptr = (unsigned char *)arpreq.arp_ha.sa_data;
//			ret = sprintf(mac, "%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5));
//		}
//	}
//	return ret;
//}
// void CPSocketUtils::DEBUG_ERROR(const char* arg1)
// {
//   //fprintf(stderr," error%s \n ",arg1);
// }
// void CPSocketUtils::DEBUG_WARNING(char*,int)
// {
//   
// }